//////////////////////////////////////////////////////////////////////////
// FileName:ucMenutree
// CreateBy:DragonXiong
// CreateDate:2009-5-4
// Description:
//		
//      
// Modify Log:090531:Change CultureInfo to SessionBox Control --Draogn
//            090602:Change SessionBox Method
//            20100114:Clear checked state of nodes, add funtion clear() --DragonXiong
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Data.Security;
using BLL = Auto.Data.Systems.BLL;
using Model = Auto.Data.Systems.Model;
using Auto.Web.Common;

namespace BaseSystem._System.UserControl
{
    public partial class ucMenutree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.trRoles.Attributes.Add("onclick", "CheckEvent()");
            }
        }

        /// <summary>
        /// Gets or sets the checked nodes.
        /// </summary>
        /// <value>The checked nodes.</value>
        public IDictionary<string, string> CheckedNodes
        {
            get
            {
                IDictionary<string, string> result = new Dictionary<string, string>();
                foreach (TreeNode trChecked in this.trRoles.CheckedNodes)
                {
                    result.Add(trChecked.Value, trChecked.Text);
                }
                return result;
            }
            set
            {
                foreach (KeyValuePair<string, string> keyValue in value)
                {
                    CheckParent(keyValue.Value);
                }
            }
        }

        /// <summary>
        /// Gets or sets the checked nodes.
        /// </summary>
        /// <value>The checked nodes.</value>
        public IDictionary<string, string> CheckedFuncNodes
        {
            get
            {
                IDictionary<string, string> result = new Dictionary<string, string>();
                foreach (TreeNode trChecked in this.trRoles.CheckedNodes)
                {
                    if (!trChecked.Value.StartsWith("OP_"))
                    {
                        result.Add(trChecked.Value, trChecked.Text);


                    }
                }
                return result;
            }
            set
            {
                Clear(); //Clear checked state of nodes
                foreach (KeyValuePair<string, string> keyValue in value)
                {
                    CheckParent(keyValue.Value);
                }
            }
        }


        /// <summary>
        /// Gets or sets the checked nodes.
        /// </summary>
        /// <value>The checked nodes.</value>
        public IDictionary<string, string> CheckedOperateNodes
        {
            get
            {
                IDictionary<string, string> result = new Dictionary<string, string>();
                foreach (TreeNode trChecked in this.trRoles.CheckedNodes)
                {
                    if (trChecked.Value.StartsWith("OP_"))
                    {
                        result.Add(trChecked.Parent.Value + "|" + trChecked.Value, trChecked.Text);

                    }
                }
                return result;
            }
            set
            {
                foreach (KeyValuePair<string, string> keyValue in value)
                {
                    string[] funcOperate = keyValue.Key.Split('|');
                    CheckParent(funcOperate[0], funcOperate[1]);
                }
            }
        }
        /// <summary>
        /// Checks the parent.
        /// </summary>
        /// <param name="value">The value.</param>
        private void CheckParent(string parentValue, string value)
        {
            foreach (TreeNode parent in this.trRoles.Nodes)
            {
                CheckChild(parent, parentValue, value);
            }
        }
        /// <summary>
        /// Checks the child.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="value">The value.</param>
        private void CheckChild(TreeNode parent, string parentValue, string value)
        {

            foreach (TreeNode child in parent.ChildNodes)
            {
                if (child.Parent.Value == parentValue && child.Value == value)
                {
                    child.Checked = true;
                }
                if (child.ChildNodes.Count > 0)
                {
                    CheckChild(child, parentValue, value);
                }
            }
        }



        /// <summary>
        /// Checks the parent.
        /// </summary>
        /// <param name="value">The value.</param>
        private void CheckParent(string value)
        {
            foreach (TreeNode parent in this.trRoles.Nodes)
            {
                CheckChild(parent, value);
            }
        }
        /// <summary>
        /// Checks the child.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="value">The value.</param>
        private void CheckChild(TreeNode parent, string value)
        {
            foreach (TreeNode child in parent.ChildNodes)
            {
                if (child.Value == value)
                {
                    child.Checked = true;
                    SetParentCheck(child);
                }
                if (child.ChildNodes.Count > 0)
                {
                    CheckChild(child, value);
                }
            }
        }

        /// <summary>
        /// Sets the parent check.
        /// </summary>
        /// <param name="tn">The tn.</param>
        private void SetParentCheck(TreeNode tn)
        {
            if (tn.Parent != null && tn.Parent.Checked == false)
            {
                tn.Parent.Checked = true;
                if (tn.Parent.Parent != null)
                {
                    SetParentCheck(tn.Parent);
                }
            }
        }

        #region --Clear Selected Nodes--
        private void Clear()
        {
            foreach (TreeNode node in trRoles.Nodes)
            {
                node.Checked = false;
                if (node.ChildNodes.Count > 0)
                {
                    ClearChild(node);
                }
            }
        }

        private void ClearChild(TreeNode tn)
        {
            foreach (TreeNode node in tn.ChildNodes)
            {
                node.Checked = false;
                if (node.ChildNodes.Count > 0)
                {
                    ClearChild(node);
                }
            }
        }
        #endregion --Clear Selected Nodes--

        #region Tree Init


        /// <summary>
        /// Trees the init.
        /// </summary>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="sysName">Name of the sys.</param>
        public void TreeInit(string sysID, string sysName)
        {
            this.trRoles.Nodes.Clear();
            TreeNode rootNode = new TreeNode(sysName, sysID);
            this.trRoles.Nodes.Add(rootNode);
            BLL.SysFunctions sysFuncs = new BLL.SysFunctions();
            DataTable topFuncs = sysFuncs.GetSysFunctionsTop(SessionBox.CultrueInfo, sysID);
            LoadRootNode(rootNode, topFuncs);
        }

        /// <summary>
        /// Loads the root node.
        /// </summary>
        /// <param name="topFuncs">The top funcs.</param>
        private void LoadRootNode(TreeNode rootNode, DataTable topFuncs/*XmlNode xmlNode*/)
        {

            TreeNode node;

            foreach (DataRow dr in topFuncs.Rows)
            {
                node = new TreeNode();
                node.Text = dr[Model.SysFunctions.FIELD_FuncName].ToString();
                node.Value = dr[Model.SysFunctions.FIELD_FuncID].ToString();
                node.SelectAction = TreeNodeSelectAction.Expand;
                rootNode.ChildNodes.Add(node);
                LoadChildTreeNodes(Convert.ToBoolean(dr[Model.SysFunctions.FIELD_IsEnd]), node, dr[Model.SysFunctions.FIELD_SysID].ToString(), dr[Model.SysFunctions.FIELD_FuncID].ToString());
            }
        }

        /// <summary>
        /// Loads the fun node.
        /// </summary>
        /// <param name="treeNode">The tree node.</param>
        /// <param name="dtsubFuncs">The dtsub funcs.</param>
        private void LoadFunNode(TreeNode treeNode, DataTable dtsubFuncs/* XmlNode xmlNode*/)
        {
            TreeNode node;

            foreach (DataRow dr in dtsubFuncs.Rows/*XmlElement xe in xmlNode.ChildNodes*/)
            {
                node = new TreeNode();
                node.Text = dr[Model.SysFunctions.FIELD_FuncName].ToString();
                node.Value = dr[Model.SysFunctions.FIELD_FuncID].ToString();


                TreeNode patNode = treeNode;

                while (patNode.Parent != null)
                {
                    patNode = patNode.Parent;
                }
                node.SelectAction = TreeNodeSelectAction.Expand;

                treeNode.ChildNodes.Add(node);

                LoadChildTreeNodes(Convert.ToBoolean(dr[Model.SysFunctions.FIELD_IsEnd]), node, dr[Model.SysFunctions.FIELD_SysID].ToString(), dr[Model.SysFunctions.FIELD_FuncID].ToString());


                LoadOperationList(node, dr[Model.SysFunctions.FIELD_SysID].ToString(), dr[Model.SysFunctions.FIELD_FuncID].ToString());
            }
        }

        /// <summary>
        /// Loads the child tree nodes.
        /// </summary>
        /// <param name="isEnd">if set to <c>true</c> [is end].</param>
        /// <param name="node">The node.</param>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="funcID">The func ID.</param>
        private void LoadChildTreeNodes(bool isEnd, TreeNode node, string sysID, string funcID)
        {
            if (!isEnd)
            {
                LoadRoleFunc(node, sysID, funcID);
            }
        }

        /// <summary>
        /// Loads the operation list.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="funcID">The func ID.</param>
        private void LoadOperationList(TreeNode node, string sysID, string funcID)
        {
            SysFuncOperate operate = new SysFuncOperate();
            DataTable dt = operate.GetRoleFunc(sysID, funcID, "");
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode childNode = new TreeNode();
                childNode.Value = dr[SysRoleFuncOperate.FIELD_OperateID].ToString();
                childNode.Text = dr[OperateList.FIELD_OperateName].ToString();
                childNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(childNode);
            }

        }

        /// <summary>
        /// Loads the role func.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="funcID">The func ID.</param>
        private void LoadRoleFunc(TreeNode node, string sysID, string funcID)
        {
            BLL.SysFunctions subFuncs = new BLL.SysFunctions();
            DataTable subSysFuncs = subFuncs.GetSysFunctionsByParentID(SessionBox.CultrueInfo, sysID.ToString(), funcID);

            LoadFunNode(node, subSysFuncs);
        }
        #endregion Tree Init
    }
}